package dkbta.processing.tasks;

import dkbta.ontology.elements.ElementDef;
import dkbta.processing.Patient;
import dkbta.processing.WorkQueue;

/**
 * A task that is used for computing local elements.<br>
 * The task is associated with a single patient.
 * 
 * @author Uri Kanonov
 */
public class LocalTask extends Task{
	/** The patient associated with the task */
	protected final Patient _patient;
	
	/**
	 * Initializes a local computation task
	 * 
	 * @param element The associated element
	 * @param isPrimary Whether the task is a primary task
	 * @param patient The patient associated with the task
	 */
	public LocalTask(ElementDef element, boolean isPrimary, Patient patient){
		super(element, patient.getPatientId(), isPrimary);
		_patient = patient;
	}

	@Override
	public boolean performTask(WorkQueue wq){
		boolean completed = element.compute(_patient, wq, this);
		if (completed){
			// Notifying all parent tasks that this child task has been completed
			notifyParentTasksAboutCompletion(wq);

			// Noting that the element has been computed
			_patient.addComputedElement(element);
			return true;
		}else{
			return false;
		}
	}	

	@Override
	public String toString(){
		return "Local: PID = " + _patientId + ", " + element + " "
				+ ((isPrimary) ? "[PRIMARY]" : "");
	}
}
